package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.AddCodeDAO;
import com.mbc.rmt.dao.AddCodeDetailDAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.AddCodeDataList;
import com.mbc.rmt.data.AddCodeNDetail;
import com.mbc.rmt.data.AddCodeNDetailDataList;
import com.mbc.rmt.entities.AddCode;
import com.mbc.rmt.entities.AddCodeDetail;
import com.mbc.rmt.entities.AddCodeRpt;

public class AddCodeMgr extends AbstractMgr {

	private AddCodeDAO addCodeDAO;
	private AddCodeDetailDAO addCodeDetailDAO;
	private LogDAO logDAO;

	public AddCodeMgr() {
	}

	public AddCodeMgr(String sessionId) {
		this.sessionId = sessionId;
	}

	public void create(AddCode entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			addCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
			entity.setAddCodeId(KeyGenerator.generateKey(getConnection()));
			addCodeDAO.create(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			Log l_logentity =  new Log();
			l_logentity.setTablename("AddCode");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getAddCodeId());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);

			AddCodeDetailDAO l_AddCodeDetail = RemittanceDAOFactory
					.getAddCodeDetailDAO(getConnection());
			for (AddCodeDetail l_detail : entity.getAddCodeDetailList()) {
				l_detail.setAddCodeId(entity.getAddCodeId());
				l_detail.setAddCodeDetailId(KeyGenerator
						.generateKey(getConnection()));
				l_AddCodeDetail.create(l_detail);
				
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setTablename("AddCodeDetail");
				l_logentity.setKey(l_detail.getAddCodeDetailId());
				logDAO.getData(l_logentity);
			}

			commintTransaction();
		} catch (SQLException e) {
			rollbackTransaction();
			if (DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e
					.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}

	public void createDetail(AddCodeDetail entity) throws Exception {
		openConnection();
		try {
			AddCodeDetailDAO l_AddCodeDetail = RemittanceDAOFactory
					.getAddCodeDetailDAO(getConnection());
			entity.setAddCodeDetailId(KeyGenerator.generateKey(getConnection()));
			l_AddCodeDetail.create(entity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public void update(AddCode entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			addCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
			addCodeDAO.update(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			Log l_logentity = new Log();
			l_logentity.setTablename("AddCode");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getAddCodeId());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
			AddCodeDetailDAO l_AddCodeDetailDAO = RemittanceDAOFactory
					.getAddCodeDetailDAO(getConnection());
			l_AddCodeDetailDAO.deleteByAddCodeId(entity.getAddCodeId());
			for (AddCodeDetail l_detail : entity.getAddCodeDetailList()) {
				l_detail.setAddCodeId(entity.getAddCodeId());
				l_detail.setAddCodeDetailId(KeyGenerator
						.generateKey(getConnection()));
				l_AddCodeDetailDAO.create(l_detail);
				
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setTablename("AddCodeDetail");
				l_logentity.setKey(l_detail.getAddCodeDetailId());
				l_logentity.setLogstatus(Log.__UPDATE_DATA);
				logDAO.getData(l_logentity);
			}
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}

	public void delete(long key, long userid) throws Exception {
		openConnection();
		try {
			beginTransaction();
			AddCodeDetailDAO l_AddCodeDetailDAO = RemittanceDAOFactory
					.getAddCodeDetailDAO(getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			
			ArrayList<AddCodeDetail> l_detail = l_AddCodeDetailDAO.getInfoList(key);
			for(int j=0; j<l_detail.size();j++){
				
				l_logentity.setTablename("AddCodeDetail");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(l_detail.get(j).getAddCodeDetailId());
				l_logentity.setUserid(userid);
				l_logentity.setLogstatus(Log.__DELETE_DATA);
				logDAO.getData(l_logentity);
			}
			
			l_AddCodeDetailDAO.delete(key);
			addCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
			
			l_logentity.setTablename("AddCode");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(key);
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			addCodeDAO.delete(key);
			commintTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}

	public AddCode find(long key) throws Exception {
		openConnection();
		try {
			addCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
			AddCode l_AddCode = addCodeDAO.find(key);
			if (l_AddCode != null) {
				AddCodeDetailDAO l_AddCodeDetailDAO = RemittanceDAOFactory
						.getAddCodeDetailDAO(getConnection());
				l_AddCode.setAddCodeDetailList(l_AddCodeDetailDAO
						.findByAddCodeId(key));
			}
			return l_AddCode;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public List<AddCode> findAll() throws Exception {
		openConnection();
		try {
			addCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
			return addCodeDAO.findAll();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public AddCodeDataList findAddCodes(AddCode cri) throws Exception {
		openConnection();
		try {
			addCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
//			AddCode l_AddCode = new AddCode();
			return addCodeDAO.findAddCodes(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public AddCodeNDetailDataList findAddCodeNDetails(AddCodeNDetail cri)
			throws Exception {
		openConnection();
		try {
			addCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());
			return addCodeDAO.findAddCodeNDetails(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	// MBC Begin
	public JasperPrint prepareAddCodeReport(AddCodeRpt info) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			addCodeDAO = RemittanceDAOFactory.getAddCodeDAO(getConnection());

			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(
					addCodeDAO.prepareAddCodeReport(info));

			JasperReport l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH
							+ "reports/AddCodeRpt.jasper");
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("companyname", "MBC");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}

	public AddCode getAddCode(String code,long branchId) throws Exception {
		openConnection();
		try {
			AddCode l_info=new AddCode();
			ArrayList<AddCodeDetail> l_detail=new ArrayList<AddCodeDetail>();
			addCodeDAO = RemittanceDAOFactory.getAddCodeDAO(dbManager
					.getConnection());
			addCodeDetailDAO=RemittanceDAOFactory.getAddCodeDetailDAO(dbManager
					.getConnection());
	        l_info=addCodeDAO.getAddCode(code,branchId);
	        l_detail=addCodeDetailDAO.getInfoList(l_info.getAddCodeId());
	        l_info.setAddCodeDetailList(l_detail);
			return l_info;
			
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public AddCode findAddCodeByCode(String code,long branchId) throws Exception {
		openConnection();
		try {
			addCodeDAO = RemittanceDAOFactory.getAddCodeDAO(dbManager
					.getConnection());
			return addCodeDAO.findByCode(code,branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	// MBC End
}
